/**
 * @author lteng
 * @time 2022-06-17 22:26:50 
 */
 
package liteng.study.cn.stuscore.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import liteng.study.cn.stuscore.comm.Page;
import liteng.study.cn.stuscore.dao.GradeDao;
import liteng.study.cn.stuscore.dao.ScoreDao;
import liteng.study.cn.stuscore.dao.StudentDao;
import liteng.study.cn.stuscore.dao.impl.GradeDaoImpl;
import liteng.study.cn.stuscore.dao.impl.ScoreDaoImpl;
import liteng.study.cn.stuscore.dao.impl.StudentDaoImpl;
import liteng.study.cn.stuscore.entity.Grade;
import liteng.study.cn.stuscore.entity.Score;
import liteng.study.cn.stuscore.entity.Student;
import liteng.study.cn.stuscore.service.StudentService;

/**
 * 学生信息业务层接口实现类
 */
public class StudentServiceImpl implements StudentService {

	private StudentDao studentDao = new StudentDaoImpl();
	
	private GradeDao gradeDao = new GradeDaoImpl();
	
	private ScoreDao scoreDao = new ScoreDaoImpl();
	
	@Override
	public void add(Student student) {
		// TODO Auto-generated method stub
		studentDao.add(student);
	}

	@Override
	public void update(Student student) {
		// TODO Auto-generated method stub
		studentDao.update(student);
		
	}

	@Override
	public void delete(Student student) {
		// TODO Auto-generated method stub
		studentDao.delete(student);
	}

	@Override
	public Student getOne(long id) {
		// TODO Auto-generated method stub
		Student student = studentDao.qryOne(id);
		return student;
	}

	@Override
	public Page getPageStudent(int pageIndex, int pageSize) {
		// TODO Auto-generated method stub
		Page page = studentDao.qryStudents(pageIndex, pageSize);
		for(Map<String, Object> map: page.getData()) {
			int gId = (int) map.get("gid");
			Grade grade = gradeDao.qryOne(gId);
			map.put("gname", grade.getName());
		}
		return page;
	}

	@Override
	public Page getPageStudent(int pageIndex, int pageSize, String name) {
		// TODO Auto-generated method stub
		Page page = studentDao.qryStudents(pageIndex, pageSize,name);
		
		for(Map<String, Object> map: page.getData()) {
			int gId = (int) map.get("gid");
			Grade grade = gradeDao.qryOne(gId);
			map.put("gname", grade.getName());
		}
		return page;
	}

	@Override
	public boolean isExistStudentById(long id) {
		// TODO Auto-generated method stub
		return studentDao.qryStudentById(id)==null?false:true;
	}

	@Override
	public Student getOne(int id) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<Map<String, Object>> getStudentsWithGradeAndScore(int gId, int eId) {
		// TODO Auto-generated method stub

		 List<Map<String, Object>> resl  =new ArrayList<Map<String,Object>>();
		 
		 List<Student> students = studentDao.qryStudentsByGId(gId);
		 
		 for(Student student:students) {
			 Map<String, Object> temp = new HashMap<String, Object>();
			 
			 temp.put("id", student.getId());
			 temp.put("name", student.getName());
			 temp.put("sex", student.getSex());
			 temp.put("age", student.getAge());
			 temp.put("gid", student.getGid());
			 Score score = scoreDao.qryScore(student.getId(), eId);
			 
			 if (score==null) {
				temp.put("score", null);
			}else {
				temp.put("score", score.getScore());
			}
			 Grade grade = gradeDao.qryOne(gId);
			 
//			 temp.put("gid", grade.getId());
			 temp.put("gname", grade.getName());
			 
			 resl.add(temp);
			 
		 }
		 return resl;
	}

	@Override
	public List<Map<String, Object>> getStudentsWithGradeAndScore(int gId, int eId, String name) {
		// TODO Auto-generated method stub
		List<Map<String, Object>> resl  =new ArrayList<Map<String,Object>>();
		 
		 List<Student> students = studentDao.qryStudentsByGIdAndName(gId,name);
		 if (students==null) {
			return null;
		}
		 for(Student student:students) {
			 Map<String, Object> temp = new HashMap<String, Object>();
			 
			 temp.put("id", student.getId());
			 temp.put("name", student.getName());
			 temp.put("sex", student.getSex());
			 temp.put("age", student.getAge());
			 temp.put("gid", student.getGid());
			 Score score = scoreDao.qryScore(student.getId(), eId);
			 
			 if (score==null) {
				temp.put("score", null);
			}else {
				temp.put("score", score.getScore());
			}
			 Grade grade = gradeDao.qryOne(gId);
			 
			 temp.put("gid", grade.getId());
			 temp.put("gname", grade.getName());
			 
			 resl.add(temp);
			 
		 }
		 return resl;
	}


}
